The jobs that cron is expected to do are stored in a file called the crontab, which you can view by typing (as root):
crontab -l(That's a small L.) When you display the crontab, you will see entries like this one:
# rotate staff diary every month 25 0 1 * * /usr/forum/bin/monthly-diary-rotate.cshwhich runs a script in /usr/forum/bin called monthly-diary-rotate.csh at 12:25 am on the first of every month.
An entry consists of two parts-- a time to run, and a command to run. (The top line beginning with a # mark is a comment, and is only there to remind people reading the file what the entry is there for.)
The time to run (the first part of the entry) is broken up into 5 fields:
A "*" in one of these fields indicates that the job should be executed at any or every one of these, so for instance a "*" in the month of the year field means that this task should be carried out every month at the time specified in the other fields. A "*" in the day of the week field, when there is something in the day of the month field, does not mean that this task should be carried out every day, but rather that it should be carried out on the appointed day of the month regardless of what weekday it is. Only "*"s in the day of the week field,and the day of the month field, and the month of the year field would indicate a daily task.
Examples: The job with this time to run would run every Sunday at 12:20 am.
20 0 * * 0This job would run at midnight on any Friday the 13th.
0 0 13 * 5
You don't have to put just one time in a field. This job, for instance,
would run every day at 12:30am, 2:30am, 4:30am, 6:30am, 6:30pm, 8:30pm, and
10:30pm:
30 0,2,4,6,18,20,22 * * *and this one would run every Tuesday and Friday at 5:30 am:
30 5 * * 2,5
The command to run is simply what you would type at the command line if you
were doing it manually.
To edit the crontab, simply type (as root):
crontab -eWhat you will actually have open and be editing is a temporary file, but as soon as you save it the crontab will reflect your changes. If you are making major changes or are worried that you might screw it up somehow you might want to save the old version like this:
crontab -l > ~yourname/crontab.old(The ">" redirects the output of a command, in this case crontab -l into a file.) There it is. Hope using cron to automate tasks makes your life easier.
Back to the System Administration tips page
Back to the UNIX Tips main page
Back to my home page